﻿#include "FITKAbaqusHDF5AdaptorInertia.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTransform.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractInertia.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInertiaPointMassInertia.h"
#include "FITKAbaqusHDF5AdaptorInertia.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorInertia::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorInertia";
    }

    bool FITKAbaqusHDF5AdaptorInertia::adaptR()
    {
        auto inertia = dynamic_cast<Interface::FITKAbaAbstractInertia*>(_dataObj);
        if (!_reader || !inertia || !_h5Group) return false;
        bool isR = true;
        isR &= readInertia(inertia, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorInertia::adaptW()
    {
        auto inertia = dynamic_cast<Interface::FITKAbaAbstractInertia*>(_dataObj);
        if (!_writer || !inertia || !_h5Group) return false;
        bool isW = true;
        isW &= writeInertia(inertia, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorInertia::readInertia(Interface::FITKAbaAbstractInertia * inertia, H5::Group & h5Group)
    {
        if (!inertia)return false;
        bool isR = readBoundaryLocation(inertia, h5Group);
        bool activeState = readBoolAttribute(h5Group, "ActiveState");
        inertia->setActiveState(activeState);

        auto eInertiaType = inertia->getInertiaType();

        if (eInertiaType == Interface::FITKAbaAbstractInertia::FITKAbaInertiaType::PointMassInertia)
        {
            isR &= readPointMassInertia(inertia, h5Group);
        }
        else if (eInertiaType == Interface::FITKAbaAbstractInertia::FITKAbaInertiaType::HeatCapacitance)
        {

        }
        else if (eInertiaType == Interface::FITKAbaAbstractInertia::FITKAbaInertiaType::NonstructuralMass)
        {

        }
        else
        {
            return false;
        }

        return isR;
    }

    bool FITKAbaqusHDF5AdaptorInertia::writeInertia(Interface::FITKAbaAbstractInertia * inertia, H5::Group & h5Group)
    {
        if (!inertia)return false;
        bool isValid = false;
        bool isW = true;


        std::string groupName = createParentAttribute(inertia, h5Group);
        if (groupName.empty())return false;

        auto inertiaGroup = h5Group.createGroup(groupName);
        auto eInertiaType = inertia->getInertiaType();
        Core::FITKEnumTransfer<Interface::FITKAbaAbstractInertia::FITKAbaInertiaType> fitkTypeTrafer;
        auto sInertiaType = fitkTypeTrafer.toStrting(eInertiaType, isValid);
        if (!isValid) return false;
        writeStrAttribute(inertiaGroup, "InertiaType", sInertiaType.toStdString());

        bool activeState = inertia->getActiveState();
        writeBoolAttribute(inertiaGroup, "ActiveState", &activeState);

        if (eInertiaType == Interface::FITKAbaAbstractInertia::FITKAbaInertiaType::PointMassInertia)
        {
            isW &= writePointMassInertia(inertia, inertiaGroup);
        }
        else if (eInertiaType == Interface::FITKAbaAbstractInertia::FITKAbaInertiaType::HeatCapacitance)
        {

        }
        else if (eInertiaType == Interface::FITKAbaAbstractInertia::FITKAbaInertiaType::NonstructuralMass)
        {

        }
        else
        {
            return false;
        }
        //写出模型信息
        return writeBoundaryLocation(inertia, inertiaGroup);
    }

    bool FITKAbaqusHDF5AdaptorInertia::readPointMassInertia(Interface::FITKAbaAbstractInertia * inertia, H5::Group & h5Group)
    {
        Interface::FITKAbaInertiaPointMassInertia* pointMassInertia = dynamic_cast<Interface::FITKAbaInertiaPointMassInertia*>(inertia);
        if (!pointMassInertia) return false;
        bool isValid = false;

        auto sMassType = readStrAttribute(h5Group, "MassType");
        Core::FITKEnumTransfer<Interface::FITKAbaInertiaPointMassInertia::MassType> fitkTypeTrafer;
        auto eMassType = fitkTypeTrafer.fromString(QString::fromStdString(sMassType), isValid);
        if (!isValid) return false;
        pointMassInertia->setMassType(eMassType);
        double isoTropicMass = readDoubleAttribute(h5Group, "IsoTropicMass");
        pointMassInertia->setIsoTropicMass(isoTropicMass);

        double m11 = readDoubleAttribute(h5Group, "M11");
        pointMassInertia->setM11(m11);
        double m22 = readDoubleAttribute(h5Group, "M22");
        pointMassInertia->setM22(m22);
        double m33 = readDoubleAttribute(h5Group, "M33");
        pointMassInertia->setM33(m33);
        bool specifyOffDiagnalTerms = readBoolAttribute(h5Group, "SpecifyOffDiagnalTerms");
        pointMassInertia->setSpecifyOffDiagnalTerms(specifyOffDiagnalTerms);
        double i11 = readDoubleAttribute(h5Group, "I11");
        pointMassInertia->setI11(i11);
        double i12 = readDoubleAttribute(h5Group, "I12");
        pointMassInertia->setI12(i12);
        double i13 = readDoubleAttribute(h5Group, "I13");
        pointMassInertia->setI13(i13);
        double i22 = readDoubleAttribute(h5Group, "I22");
        pointMassInertia->setI22(i22);
        double i23 = readDoubleAttribute(h5Group, "I23");
        pointMassInertia->setI23(i23);
        double i33 = readDoubleAttribute(h5Group, "I33");
        pointMassInertia->setI33(i33);
        double alpha = readDoubleAttribute(h5Group, "Alpha");
        pointMassInertia->setAlpha(alpha);
        double composite = readDoubleAttribute(h5Group, "Composite");
        pointMassInertia->setComposite(composite);
        int csysId = readIntAttribute(h5Group, "CsysId");
        pointMassInertia->setCsysId(csysId);
        return true;
    }

    bool FITKAbaqusHDF5AdaptorInertia::writePointMassInertia(Interface::FITKAbaAbstractInertia * inertia, H5::Group & h5Group)
    {
        Interface::FITKAbaInertiaPointMassInertia* pointMassInertia = dynamic_cast<Interface::FITKAbaInertiaPointMassInertia*>(inertia);
        if (!pointMassInertia) return false;
        bool isValid = false;

        auto eMassType = pointMassInertia->getMassType();
        Core::FITKEnumTransfer<Interface::FITKAbaInertiaPointMassInertia::MassType> fitkTypeTrafer;
        auto sMassType = fitkTypeTrafer.toStrting(eMassType, isValid);
        if (!isValid) return false;
        writeStrAttribute(h5Group, "MassType", sMassType.toStdString());

        double isoTropicMass = pointMassInertia->getIsoTropicMass();
        writeDoubleAttribute(h5Group, "IsoTropicMass", &isoTropicMass);

        double m11 = pointMassInertia->getM11();
        writeDoubleAttribute(h5Group, "M11", &m11);
        double m22 = pointMassInertia->getM22();
        writeDoubleAttribute(h5Group, "M22", &m22);
        double m33 = pointMassInertia->getM33();
        writeDoubleAttribute(h5Group, "M33", &m33);
        bool specifyOffDiagnalTerms = pointMassInertia->getSpecifyOffDiagnalTerms();
        writeBoolAttribute(h5Group, "SpecifyOffDiagnalTerms", &specifyOffDiagnalTerms);
        double i11 = pointMassInertia->getI11();
        writeDoubleAttribute(h5Group, "I11", &i11);
        double i12 = pointMassInertia->getI12();
        writeDoubleAttribute(h5Group, "I12", &i12);
        double i13 = pointMassInertia->getI13();
        writeDoubleAttribute(h5Group, "I13", &i13);
        double i22 = pointMassInertia->getI22();
        writeDoubleAttribute(h5Group, "I22", &i22);
        double i23 = pointMassInertia->getI23();
        writeDoubleAttribute(h5Group, "I23", &i23);
        double i33 = pointMassInertia->getI33();
        writeDoubleAttribute(h5Group, "I33", &i33);
        double alpha = pointMassInertia->getAlpha();
        writeDoubleAttribute(h5Group, "Alpha", &alpha);
        double composite = pointMassInertia->getComposite();
        writeDoubleAttribute(h5Group, "Composite", &composite);
        int csysId = pointMassInertia->getCsysId();
        writeIntAttribute(h5Group, "CsysId", &csysId);
        return true;
    }

}


